﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZDSoft.UPMS.Model.SystemPart;
using ZDSoft.UPMS.Core;
using ZDSoft.UPMS.Service;

namespace ZDSoft.UPMS.Web.App
{
    public class AppHelper
    {
        //使用MD5加密传入的字符
        public static string EncodeMd5(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
        }

        //保存登录用户的信息
        public static User  LoginedUser
        {
            get
            {
                //如果Session信息不为空
                if (HttpContext.Current.Session["LoginedUser"] != null)
                {
                    //将Session登录信息转换为User并返回
                    return HttpContext.Current.Session["LoginedUser"] as User;
                }
                return null;
            }
            set
            {
                value.Password = "";
                //将传入的信息保存到Session中
                HttpContext.Current.Session["LoginedUser"] = value;
            }
        }


        //菜单权限
        public static IList<SystemFunction> Privileges
        {
            get
            {
                IList<SystemFunction> list = null;

                if (LoginedUser == null)
                {
                    return list;
                }
                list = new List<SystemFunction>();

                foreach (Role role in LoginedUser.Roles)
                {
                    foreach (SystemFunction Function in role.SystemFunctions)
                    {
                        list.Add(Function);
                    }
                }
                return list;
            }
        }

    }
}